/*
 * FaceCat图形通讯框架(非开源)
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "stdafx.h"
#include "FCRadioButton.h"

namespace FaceCat{
    FCRadioButton::FCRadioButton(){
    }
    
    FCRadioButton::~FCRadioButton(){
    }
    
    String FCRadioButton::getGroupName(){
        return m_groupName;
    }
    
    void FCRadioButton::setGroupName(const String& value){
        m_groupName = value;
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    String FCRadioButton::getViewType(){
        return L"RadioButton";
    }
    
    void FCRadioButton::getAttribute(const String& name, String *value, String *type){
        if(name == L"groupname"){
            *type = L"text";
            *value = getGroupName();
        }
        else{
            FCCheckBox::getAttribute(name, value, type);
        }
    }
    
    ArrayList<String> FCRadioButton::getAttributeNames(){
        ArrayList<String> attributeNames = FCCheckBox::getAttributeNames();
        attributeNames.add(L"GroupName");
        return attributeNames;
    }
    
    void FCRadioButton::onClick(FCTouchInfo touchInfo){
        if(!isChecked()){
            setChecked(!isChecked());
        }
        callTouchEvents(L"onclick", touchInfo);
        invalidate();
    }
    
    void FCRadioButton::onPaintCheckButton(FCPaint *paint, const FCRect& clipRect){
        String bkImage = getPaintingBackImage();
        if ((int)bkImage.length() > 0){
            paint->drawImage(bkImage, clipRect);
        }
        else{
            if(isChecked()){
                FCRect innerRect ={clipRect.left + 2, clipRect.top + 2, clipRect.right - 2, clipRect.bottom - 2};
                if (clipRect.right - clipRect.left < 4 || clipRect.bottom - clipRect.top < 4){
                    innerRect = clipRect;
                }
                paint->fillEllipse(getPaintingButtonBackColor(), innerRect);
            }
            paint->drawEllipse(getPaintingButtonBorderColor(), 1, 0, clipRect);
        }
    }
    
    void FCRadioButton::setAttribute(const String& name, const String& value){
        if(name == L"groupname"){
            setGroupName(value);
        }
        else{
            FCCheckBox::setAttribute(name, value);
        }
    }
    
    void FCRadioButton::update(){
        if(m_native && !m_native->m_lockUpdate){
            if (isChecked()){
                ArrayList<FCView*> views;
                if (getParent()){
                    views = getParent()->m_views;
                }
                else{
                    views = getNative()->m_views;
                }
                for(int c = 0; c < views.size(); c++){
                    FCRadioButton *radioButton = (FCRadioButton*)dynamic_cast<FCRadioButton*>(views.get(c));
                    if(radioButton && radioButton != this){
                        if (radioButton->getGroupName() == getGroupName() && radioButton->isChecked()){
                            radioButton->setChecked(false);
                            radioButton->invalidate();
                        }
                    }
                }
            }
        }
    }
}
